using System;
using System.Collections.Generic;
using System.Web.WebPages;

namespace Eksponent.RazorRocks
{
    public interface ITemplateController<TItem>
    {
        /// <summary>
        /// Gets the template action to render the item. The parameters can be changed before, and these will be passed to the render action
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="parameters">The parameters.</param>
        /// <returns></returns>
        TemplateAction<TItem> GetAction(TItem item, IDictionary<string, object> parameters);
    }


    public class TemplateController<TItem> : ITemplateController<TItem>
    {

        public Func<TItem, IDictionary<string, object>, TemplateAction<TItem>> Action { get; set; }

        public TemplateController(Func<TItem, IDictionary<string, object>, TemplateAction<TItem>> action)
        {
            Action = action;
        }

        public TemplateAction<TItem> GetAction(TItem item, IDictionary<string, object> parameters)
        {
            return Action(item, parameters);
        }

        public static implicit operator TemplateController<TItem>(string templateName)
        {
            return new TemplateController<TItem>((item, ps) => templateName);
        }

        public static implicit operator TemplateController<TItem>(TemplateAction<TItem> action)
        {
            return new TemplateController<TItem>((item, ps) => action);
        }
    }
}